package net.maku.newbase;

import org.apache.http.HttpStatus;

import java.util.HashMap;

/**
 * 接口返回数据
 *
 * @author
 * @date
 */
public class R extends HashMap<String, Object> {
    private final static String message = "msg";
    private final static String status = "code";
    private final static String data = "data";

    private static final long serialVersionUID = 1L;

    public R() {
        put(status, 0);
        put(message, "success");
        put(data, null);
    }

    public R(RE re) {
        put(status, re.getCode());
        put(message, re.getMsg());
        put(data, null);
    }

    public static R error() {
        return error(HttpStatus.SC_INTERNAL_SERVER_ERROR, "未知异常，请联系管理员");
    }

    public static R error(RE re) {
        return new R(re);
    }

    public static R error(String msg) {
        return error(HttpStatus.SC_INTERNAL_SERVER_ERROR, msg);
    }

    public static R error(int code, String msg) {
        R r = new R();
        r.put(status, code);
        r.put(message, msg);
        return r;
    }

    public static R ok() {
        return new R();
    }

    public static R ok(String msg) {
        R r = new R();
        r.put(message, msg);
        return r;
    }

    public static R ok(Object obj) {
        return new R().put(data, obj);
    }

    public static R ok(String msg, Object obj) {
        R r = new R();
        r.put(message, msg);
        r.put(data, obj);
        return r;
    }

    @Override
    public R put(String key, Object value) {
        super.put(key, value);
        return this;
    }
}
